package com.example.wx.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 普通对象处理工具类
 */
//@Slf4j
public class MyBeanUtils {

    public static  <T,S> T copy(S src, Class<T> clazz){
        if(null == src){
            return null;
        }
        T t = BeanUtils.instantiateClass(clazz);
        BeanUtils.copyProperties(src,t);
        return t;
    }

    public static  <T,S> List<T> copyList(Collection<S> srcs, Class<T> clazz){
        List<T> tList = new ArrayList<>();
        for (S s:srcs){
            T t = BeanUtils.instantiateClass(clazz);
            BeanUtils.copyProperties(s,t);
            tList.add(t);
        }
        return tList;
    }
    public static  <T,S> List<T> copyListIgnoreNull(Collection<S> srcs, Class<T> clazz){
        List<T> tList = new ArrayList<>();
        if(CollectionUtils.isEmpty(srcs)){
            return tList;
        }
        for (S s:srcs){
            T t = BeanUtils.instantiateClass(clazz);
            BeanUtils.copyProperties(s,t);
            tList.add(t);
        }
        return tList;
    }
    /**
     * fastJsont转对象
     * @param src
     * @param clazz
     * @param <T>
     * @param <S>
     * @return
     */
    public static  <T,S> T fastJsonParseObject(S src, Class<T> clazz){
        if(null == src){
            return null;
        }
        T t = JSON.parseObject(JSON.toJSONString(src), new TypeReference<T>() {});
        return t;
    }

}
